<template>
  <div class="app-container" v-if="headerBox">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="业主姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入业主姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="身份证号码" prop="card">
        <el-input v-model="queryParams.card" placeholder="请输入身份证号码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['hirer:hirer:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['hirer:hirer:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['hirer:hirer:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['hirer:hirer:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="hirerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="公司名" align="center" prop="company" />
      <el-table-column label="业主姓名" align="center" prop="name" />
      <el-table-column label="身份证号码" align="center" prop="card" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="帐号状态" align="center" prop="state">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.state==0" >
            <dict-tag :options="dict.type.zhwy_rent" :value="scope.row.state" />
          </el-tag>
           <el-tag v-if="scope.row.state==1" type="danger">
             <dict-tag :options="dict.type.zhwy_rent" :value="scope.row.state" />
           </el-tag>

        </template>
      </el-table-column>
      <el-table-column label="租赁的商铺" align="center" prop="rentCount" />
      <el-table-column label="拥有的商铺" align="center" prop="haveCount" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['hirer:hirer:edit']">修改</el-button>
          <el-button size="mini" type="text" @click="handleMoney(scope.row)">明细</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改租户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="公司名" prop="company">
          <el-input v-model="form.company" placeholder="请输入公司名" />
        </el-form-item>
        <el-form-item label="业主姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入业主姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="card">
          <el-input v-model="form.card" placeholder="请输入身份证号码" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option v-for="dict in dict.type.sys_user_sex" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="帐号状态" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio v-for="dict in dict.type.zhwy_rent" :key="dict.value"
              :label="parseInt(dict.value)">{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
  <!-- 明细弹出框 -->
  <div class="app-container" v-else>
    <el-tabs v-model="activeName">
       <el-button style="margin-left: 93%;" @click="back">返回</el-button>
      <el-tab-pane label="业主信息" name="first">

        <div class="hirer-header">
          <el-card class="box-card1">
            <div slot="header" class="clearfix">
              <span>业主信息</span>
            </div>
            <div class="hirer-hb">
              <div class="text"><el-icon name="user-solid" />
                <span class="a">业主姓名</span>
                <span class="b">{{form.name}}</span>
              </div>
              <div class="text"><el-icon name="s-order" />
                <span class="a">手机号码</span>
                <span class="b">{{form.phone}}</span>
              </div>
              <div class="text"><el-icon name="c-scale-to-original" />
                <span class="a">身份证号</span>
                <span class="b">{{form.card}}</span>
              </div>
              <div class="text"><el-icon name="message" />
                <span class="a">用户邮箱</span>
                <span class="b">{{form.email}}</span>
              </div>
              <div class="text hh"><el-icon name="date" />
                <span class="a">创建日期</span>
                <span class="b">{{form.createTime}}</span>
              </div>
            </div>

          </el-card>
          <el-card class="box-card2">

            <div slot="header" class="clearinformation">
              <span>基本资料</span>
              <hr style="color: gray; opacity: 0.4;">
              <el-tabs v-model="activeName2">
                <el-tab-pane label="商铺信息" name="first">
                  <el-table :data="newList" stripe style="width: 100%;">
                    <el-table-column prop="shopsCode" label="商户编号" width="200">
                    </el-table-column>
                    <el-table-column prop="type" label="类型" width="200">
                      <el-tag> {{this.townType}}</el-tag>
                    </el-table-column>
                    <el-table-column prop="startTime" label="开始时间"  width="200">
                    </el-table-column>
                    <el-table-column prop="endTime" label="结束时间">
                    </el-table-column>
                  </el-table>
                </el-tab-pane>
                <el-tab-pane label="历史商铺信息" name="second">
                  <el-table :data="newListBefore" stripe style="width: 100%;">
                    <el-table-column prop="shopsCode" label="商户编号" width="200">
                    </el-table-column>
                    <el-table-column prop="type" label="类型" width="200">
                      <el-tag> {{this.townType}}</el-tag>
                    </el-table-column>
                    <el-table-column prop="startTime" label="开始时间"  width="200">
                    </el-table-column>
                    <el-table-column prop="endTime" label="结束时间">
                    </el-table-column>
                  </el-table>
                </el-tab-pane>
                <el-tab-pane label="代缴费信息" name="third">
              <el-table v-loading="loading" :data="pendList" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="单号" align="center" prop="billNumber" />
              <el-table-column label="收费项目" align="center" prop="name">
              </el-table-column>
              <el-table-column label="开始日期" align="center" prop="startTime" />
              <el-table-column label="结束日期" align="center" prop="endTime" />
              <el-table-column label="金额" align="center" prop="money" />
              <el-table-column label="实时金额" align="center" prop="fundsReceived" />
              <el-table-column label="待教金额" align="center" prop="subtract" />
              </el-table>
  </el-tab-pane>
                <el-tab-pane label="历史缴费信息" name="fourth">
                <el-table v-loading="loading" :data="pendList1" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="单号" align="center" prop="billNumber" />
              <el-table-column label="收费项目" align="center" prop="name">
              </el-table-column>
              <el-table-column label="开始日期" align="center" prop="startTime" />
              <el-table-column label="结束日期" align="center" prop="endTime" />
              <el-table-column label="金额" align="center" prop="money" />
              <el-table-column label="实时金额" align="center" prop="fundsReceived" />
              <el-table-column label="待教金额" align="center" prop="subtract" />
              </el-table>
                </el-tab-pane>
              </el-tabs>
            </div>
          </el-card>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
  import {
    listHirer,
    getHirer,
    delHirer,
    addHirer,
    updateHirer
  } from "@/api/hirer/hirer";
  import { listRentalcontract, getRentalcontract,getpend,getpend1} from "@/api/feeSettings/rentalcontract";

  export default {
    name: "Hirer",
    dicts: ['zhwy_rent', 'sys_user_sex'],
    data() {
      return {
        currentBeijingTime:'',
        newListBefore: [],
        newList:[],
        rentalcontractList:[],
        pendList:[],
        pendList1:[],
        //商铺信息类型
        townType:'',
        //商铺信息数据
        townData: [],
        //业主信息标签页
        activeName: 'first',
        //基本资料标签页
        activeName2: 'first',
        openMoney: false,
        //业主信息显示与隐藏
        headerBox: true,
        //明细信息显示与隐藏
        bottom: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 租户信息表格数据
        hirerList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          card: null,
          phone: null,
          hirerId: 0,
        },
        //合同参数
        queryParamsCon: {
          pageNum: 1,
          pageSize: 10,
          shopsId: null,
          type: null,
          hirerId: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          company: [{
            required: true,
            message: "公司名不能为空",
            trigger: "blur"
          }],
          name: [{
            required: true,
            message: "业主姓名不能为空",
            trigger: "blur"
          }],
          card: [{
            required: true,
            message: "身份证号码不能为空",
            trigger: "blur"
          }],
          phone: [{
            required: true,
            message: "手机号不能为空",
            trigger: "blur"
          }],
          email: [{
            required: true,
            message: "邮箱不能为空",
            trigger: "blur"
          }],
          sex: [{
            required: true,
            message: "性别【关联数据字典】不能为空",
            trigger: "change"
          }],
          state: [{
            required: true,
            message: "帐号状态【关联数据字典】不能为空",
            trigger: "change"
          }],
        }
      };
    },
    created() {
      this.getList();
      this.getCurrentBeijingTime();
    },
    methods: {
    getCurrentBeijingTime() {
          const now = new Date(); // 假设这是UTC时间
          // 这里需要添加时区偏移量来转换为北京时间（东八区）
          now.setHours(now.getHours() + 8);
           this.currentBeijingTime =now;

      },
      back() {
        this.headerBox=true
      },
      //基本资料，商铺信息按钮
      townInformation(){
          listRentalcontract(this.queryParams).then(response => {
            this.townData = response.rows;
            for(let it of this.townData){
              if(it.type==0){
                  this.townType='在租'
              }else{
                this.townType='拥有'
              }
            }
            for(let it of this.townData){
              if(new Date(it.endTime) < this.currentBeijingTime){
                  // 如果endTime大于当前时间，则将该对象添加到新列表中
                  this.newListBefore.push(it);
              }
              if(new Date(it.endTime) > this.currentBeijingTime){
                  // 如果endTime大于当前时间，则将该对象添加到新列表中
                  this.newList.push(it);
              }
            }

          });

      },

      /** 明细按钮操作 */
        handleMoney(row) {
          this.newList=[];
          this.newListBefore=[];
        // console.log(row);
        this.queryParams.hirerId=row.id
        this.reset();
        const id = row.id || this.ids
        getHirer(id).then(response => {
          this.form = response.data;
          this.townInformation();
        });
        getpend(id).then(response => {
          this.pendList = response.data;
          //this.townInformation();
        });
        getpend1(id).then(response => {
          this.pendList1 = response.data;
          //this.townInformation();
        });

        this.headerBox = false;
      },
      /** 查询租户信息列表 */
      getList() {
        this.loading = true;
        listHirer(this.queryParams).then(response => {
          this.hirerList = response.rows;
          this.total = response.total;
          this.loading = false;
          for(let item=0;item<this.hirerList.length;item++){
             listRentalcontract(this.queryParamsCon).then(response => {
            this.rentalcontractList = response.rows;
            for(let it of this.rentalcontractList){
               if(this.hirerList[item].id == it.hirerId){
                      if(it.type==0){
                       this.hirerList[item].rentCount++;
                      }else{
                       this.hirerList[item].haveCount++;
                 }
               }
            }
          });
          }
        });
        console.log(this.selectLeverList);

      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          company: null,
          name: null,
          card: null,
          phone: null,
          email: null,
          sex: null,
          state: 0,
          rentCount: null,
          haveCount: null,
          remark: null,
          createTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加租户信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getHirer(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改租户信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateHirer(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addHirer(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除租户信息编号为"' + ids + '"的数据项？').then(function() {
          return delHirer(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('hirer/hirer/export', {
          ...this.queryParams
        }, `hirer_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
<style>
  .a {
    margin-left: 10px;
  }

  .b {
    float: right;
  }

  .text {
    font-size: 16px;
    padding-right: 10px;
    margin: 0px 10px;
    height: 50px;
    border-top: 1px solid gainsboro;
    border-left: 0px;
    border-right: 0px;
    align-items: center;
    line-height: 50px;
  }

  .hh {
    border-bottom: 1px solid gainsboro;
  }

  .item {
    margin-bottom: 18px;
  }

  .clearfix:before,
  .clearfix:after {
    display: table;
    content: "";
  }

  .clearfix:after {
    clear: both
  }

  .hirer-hb {
    margin-top: 25px;
    ;
  }

  .box-card1 {
    width: 380px;
    height: 400px;
  }

  .box-card2 {
    width: 1000px;
    height: 300px;
    margin-left: 30px;
  }

  .hirer-header {
    margin-left: 20px;
    margin-top: 20px;
    display: flex;
  }
</style>
